package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.ResultVo;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQueryVo;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.exceptionHandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-12-02
 */
@RestController
@CrossOrigin
@RequestMapping("/eduservice/edu-teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;
    //1.查询讲师表所有数据
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public ResultVo findAllTeacher(){
        //调用service方法实现查询所有
        //调用teacherService的list方法，传入null表示没有查询条件，因此将获取所有未被删除的教师记录。
        List<EduTeacher> list = teacherService.list(null);
        return ResultVo.ok().data("items",list);
    }

    //2.逻辑删除讲师
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public ResultVo removeTeacher(@PathVariable("id") String id){
        boolean flag = teacherService.removeById(id);
        if(flag){
            return ResultVo.ok();
        }
        else{
            return ResultVo.error();
        }
    }
    //3.分页查询讲师方法
    //current当前页
    //limit每页显示行中的记录总数
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="current",value = "当前页"),
            @ApiImplicitParam(name="limit",value = "每页记录")
    })

    @GetMapping("pageTeacher/{current}/{limit}")
    //1.输入需要分的页数和每页显示的行数
    //2.把页树和行数通过page方法执行查询工作
    //SELECT * FROM edu_teacher WHERE is_deleted = 0 LIMIT x, y;
    //3.获取总的行数total
    //4.将查询到的行数通过list放到records中
    //5.把total和records给map
    //6。通过调用ResultVo.ok()把map放入data中输出
    public ResultVo pageListTeacher(@PathVariable Long current,@PathVariable Long limit){
        //创建page对象
        Page<EduTeacher> teacherPage =new Page<>(current,limit);
        //调用方法实现分页
        //调用方法时，底层封装，把分页所有数据封装到teacherPage对象里面
        teacherService.page(teacherPage,null);
        //总记录数
        long total = teacherPage.getTotal();
        //数据list集合
        List<EduTeacher> records = teacherPage.getRecords();
        Map map=new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return ResultVo.ok().data(map);

    }
    //4.带条件查询的分页方法
    // 带条件分页查询讲师
    @ApiOperation(value = "带条件分页查询讲师")
    @ApiImplicitParams({
            @ApiImplicitParam(name="current",value = "当前页"),
            @ApiImplicitParam(name="limit",value = "每页记录")
    })
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public ResultVo pageTeacherCondition(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false) TeacherQueryVo teacherQueryVo){
        //创建page对象
        Page<EduTeacher> teacherPage =new Page<>(current,limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询
        String name = teacherQueryVo.getName();
        Integer level = teacherQueryVo.getLevel();
        String begin = teacherQueryVo.getBegin();
        String end = teacherQueryVo.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(level!=null){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create", begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create", end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        //调用方法实现分页
        //调用方法时，底层封装，把分页所有数据封装到teacherPage对象里面
        teacherService.page(teacherPage,wrapper);
        //总记录数
        long total = teacherPage.getTotal();
        //数据list集合
        List<EduTeacher> records = teacherPage.getRecords();
        Map map=new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return ResultVo.ok().data(map);
    }
    //5添加讲师接口
    //添加讲师接口
    @PostMapping("addTeacher")
    //@RequestBody注解用于将请求体中的JSON数据自动转换为EduTeacher对象。
    // 前端在调用时需要以JSON格式发送教师信息。
    public ResultVo addTeacher(@RequestBody EduTeacher eduTeacher){
        //调用teacherService的save方法，将eduTeacher对象保存到数据库中
        // 。该方法返回一个布尔值，表示保存是否成功。
        boolean save = teacherService.save(eduTeacher);
        if(save){
            return ResultVo.ok();
        }else {
            return ResultVo.error();
        }
    }
    //6.根据讲师id进行查询
    @GetMapping("/getTeacher/{id}")
    public ResultVo getTeacher(@PathVariable String id){
        EduTeacher eduTeacher = teacherService.getById(id);
        return ResultVo.ok().data("teacher",eduTeacher);
    }

    //7.讲师修改功能
    @ApiOperation(value = "讲师修改功能")
    @PutMapping("/updateTeacher")
    public ResultVo updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag=teacherService.updateById(eduTeacher);
        if(flag){
            return ResultVo.ok();
        }
        else{
            return ResultVo.error();
        }
    }
    //自定义异常
    @ExceptionHandler(GuliException.class)
    @ResponseBody
    public ResultVo error(GuliException e){
        e.printStackTrace();

        return ResultVo.error().code(e.getCode()).message(e.getMsg());
    }
}

